/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-16<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.controller.tool;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-16<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@RestController
@RequestMapping(value = "toolInfo")
public class ToolsController {
    private static final Logger logger = LoggerFactory.getLogger(ToolsController.class);
    
    @Autowired
    private ObjectMapper objectMapper;
    
    @PostMapping(value = "jsonFormat")
    public Object jsonFormat(@RequestParam(value = "jsonSource",required = false,defaultValue = "") String jsonSource) {
        logger.info("JSON格式化数据:{}",jsonSource);
        StringBuffer error = new StringBuffer();
        if(StringUtils.isNotBlank(jsonSource)) {
            try {
                
                return objectMapper.readTree(jsonSource);
            } catch (JsonProcessingException e) {
                logger.error("JSON格式化错误，原始数据为:{},",jsonSource);
                error.append("json数据格式化出错:"+e.getMessage());
            } catch (IOException e) {
                logger.error("JSON格式化错误，原始数据为:{},",jsonSource);
                error.append("json数据格式化出错:"+e.getMessage());
            }
        }else {
            error.append("json数据不能为空");
        }
        return error.toString();
    }
}
